package com.wjk.cms.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    private static final String PATTERN_DATE_TIME = "yyyy-MM-dd HH:mm:ss";

    private static final String PATTERN_DATE = "yyyy-MM-dd";

    private static final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 格式化日期，包含时间部分
     */
    public static String formatDateTime(Date date) {
        return dateTimeFormat.format(date);
    }

    /**
     * 格式化日期，包含时间部分 - 现在的时间
     */
    public static String formatDateTime() {
        return dateTimeFormat.format(new Date());
    }

    /**
     * 格式化日期，仅包含日期部分
     */
    public static String formatDate(Date date) {
        return dateFormat.format(date);
    }

    /**
     * 格式化日期，仅包含日期部分 - 现在的时间
     */
    public static String formatDate() {
        return dateFormat.format(new Date());
    }

    /**
     * 当前日期
     */
    public static String getDate() {
        return getDate(null, 0);
    }

    public static String getDate(int offset) {
        return getDate(null, offset);
    }

    /**
     * 根据offset返回日期
     */
    public static String getDate(String date, int offset) {
        Calendar calendar = Calendar.getInstance();
        try {
            if (StringUtils.isNotEmpty(date)) {
                calendar.setTime(dateFormat.parse(date));
            }
        } catch (ParseException e) {
        }
        calendar.add(Calendar.DATE, offset);
        return formatDate(calendar.getTime());
    }

    public static Date getNewDate(Date date, int offset) {
        Calendar calendar = Calendar.getInstance();

            if (date !=null ) {
                calendar.setTime(date);
            }

        calendar.add(Calendar.DATE, offset);
        return calendar.getTime();
    }
    //1: year 2: month other: day  获取的日期 时分秒 为23:59:59
    public static   Date getNewDate(Date date, int offset,int unit) {
        Calendar calendar = Calendar.getInstance();

        if (date !=null ) {
            calendar.setTime(date);
        }

        calendar.add(unit, offset);

        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 某月最后一天
     */
    public static String getLastDayOfMonth(String year, String month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1); // 先把日期设置成1号以免在修改月份分之后日期值不合法导致未知结果
        calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return formatDate(calendar.getTime());
    }

    public static String endOfDay(String dateStr) {
        return dateStr + " 23:59:59";
    }

    public static String beginningOfDay(String dateStr) {
        return dateStr + " 00:00:00";
    }

    public static String getStartDateOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1); // 先把日期设置成1号以免在修改月份分之后日期值不合法导致未知结果
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        return formatDate(calendar.getTime());
    }

    public static String getEndDateOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1); // 先把日期设置成1号以免在修改月份分之后日期值不合法导致未知结果
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return formatDate(calendar.getTime());
    }

    public static String getStartDateOfCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);
        return formatDate(calendar.getTime());
    }

    public static String getEndDateOfCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return formatDate(calendar.getTime());
    }

    public static String getStartDateOfYear(String year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        return formatDate(calendar.getTime());
    }

    public static String getEndDateOfYear(String year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1); // 先把日期设置成1号以免在修改月份分之后日期值不合法导致未知结果
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return formatDate(calendar.getTime());
    }

    public static String getStartDateOfMonth(String year, String month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        return formatDate(calendar.getTime());
    }

    public static String getEndDateOfMonth(String year, String month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1); // 先把日期设置成1号以免在修改月份分之后日期值不合法导致未知结果
        calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return formatDate(calendar.getTime());
    }

    public static int getMaxDateOfMonth(String year, String month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1); // 先把日期设置成1号以免在修改月份分之后日期值不合法导致未知结果
        calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        return calendar.getActualMaximum(Calendar.DATE);
    }

    //根据日期字符串 和 格式字符串返回long时间 若formatPatten为空 默认格式yyyy-MM-dd HH:mm:ss
    public static  long getTimes(String dateStr,String formatPatten){

        if (StringUtils.isNotEmpty(dateStr)) {
            formatPatten = StringUtils.isBlank(formatPatten)?PATTERN_DATE_TIME:formatPatten;
            try {
                if(formatPatten.length()==10){
                    dateStr = dateStr.substring(0,10);
                }
                Date date =new SimpleDateFormat(formatPatten).parse(dateStr);
                return date.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }


        return -1;
    }

}
